home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 October / enter-2005-10.iso / files / jedit42install.exe / {app} / macros / Java / Get_Package_Name.bsh < prev    next >
Encoding:
Text File  |  2004-08-29  |  3.7 KB  |  137 lines

  1. /*
  2.  * Get_Package_Name.bsh - a BeanShell macro script for the
  3.  * jEdit text editor - insert package name based upon path
  4.  * of current buffer
  5.  * Copyright (C) 2001 John Gellene
  6.  * jgellene@nyc.rr.com
  7.  * http://community.jedit.org
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  *
  23.  * Based on code contributed by Richard Wan
  24.  *
  25.  * $Id: Get_Package_Name.bsh,v 1.4 2004/03/19 15:58:00 spestov Exp $
  26.  *
  27.  * Checked for jEdit 4.0 API
  28.  *
  29.  */
  30.  
  31. boolean testClassPath()
  32. {
  33.     classpath = System.getProperty("java.class.path");
  34.     classSeparator = (File.separatorChar == '/' ? ':' : ';');
  35.     return (classpath.indexOf(classSeparator) != -1)
  36.         || (!classpath.endsWith("jedit.jar"));
  37. }
  38.  
  39.  
  40. File getCanonicalFile(File file)
  41. {
  42.     try
  43.     {
  44.         return new File(file.getCanonicalPath());
  45.     }
  46.     catch(IOException e)
  47.     {
  48.         return new File(file.getAbsolutePath());
  49.     }
  50. }
  51.  
  52. File getRoot(File file)
  53. {
  54.     tokens = new StringTokenizer(System.getProperty("java.class.path"),
  55.         File.pathSeparator);
  56.     fileSet = new Hashtable();
  57.     while(tokens.hasMoreTokens())
  58.     {
  59.         String tok = tokens.nextToken();
  60.         fileSet.put(getCanonicalFile(new File(tok)), tok);
  61.     }
  62.  
  63.     while(file != null)
  64.     {
  65.         if(fileSet.get(getCanonicalFile(file)) != null)
  66.             break;
  67.         parent = file.getParent();
  68.         file = (parent != null) ? new File(parent) : null;
  69.     }
  70.     return file;
  71. }
  72.  
  73.  
  74. String determinePackageName(String path)
  75. {
  76.     pathFile = new File(buffer.getPath());
  77.     File root = getRoot(pathFile);
  78.     if(root == null)
  79.         return null;
  80.     parent = pathFile.getParent();
  81.     packagePath = parent.substring(root.getPath().length(), parent.length());
  82.     packagePath = packagePath.replace(File.separatorChar, '.');
  83.     if (packagePath.endsWith("."))
  84.     {
  85.         packagePath = packagePath.substring(0, packagePath.length() - 1);
  86.     }
  87.     if (packagePath.startsWith("."))
  88.     {
  89.         packagePath = packagePath.substring(1, packagePath.length());
  90.     }
  91.     return packagePath;
  92. }
  93.  
  94. if( buffer.isReadOnly() )
  95.     Macros.error( view, "Buffer is read-only." );
  96. else
  97. {
  98.     // main routine
  99.     if(!testClassPath())
  100.     {
  101.         Macros.error(view, "This macro will not work when the Java interpreter\n"
  102.             + "loads jEdit with the '-jar' command line option.");
  103.     }
  104.     else
  105.     {
  106.         result = determinePackageName(buffer.getPath());
  107.         if(result == null)
  108.         {
  109.             Macros.error(view, "Could not find a package name.");
  110.         }
  111.         else textArea.setSelectedText(result);
  112.     }
  113. }
  114.  
  115. /*
  116.     Macro index data (in DocBook format)
  117.  
  118. <listitem>
  119.     <para><filename>Get_Package_Name.bsh</filename></para>
  120.     <abstract><para>
  121.         Inserts a plausible Java package name for the current buffer.
  122.     </para></abstract>
  123.     <para>
  124.         The macro compares the buffer's path name with the elements of the
  125.         classpath being used by the jEdit session.  An error message will be
  126.         displayed if no suitable package name is found. This macro will not
  127.         work if jEdit is being run as a JAR file without specifying a
  128.         classpath.  In that case the classpath seen by the macro consists
  129.         solely of the JAR file.
  130.     </para>
  131. </listitem>
  132.  
  133. */
  134.  
  135. // end Get_Package_Name.bsh
  136.  
  137.